﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class AddNews : WebPart
    {
        protected global::FredCK.FCKeditorV2.FCKeditor FCKeditorBody;
        protected global::FredCK.FCKeditorV2.FCKeditor FCKeditorShortContent;
        public override string WebPartId
        {
            get
            {
                return "AddNews";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Thêm mới tin tức");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }


        public override void LoadSubControls(System.Web.UI.WebControls.PlaceHolder pl, System.Web.UI.Page Page, LoadParamCls LoadParamCls)
        {
            string GroupNewsId = (string)System.Web.HttpContext.Current.Request["GroupNewsId"];
            string GroupNewsItemId = (string)System.Web.HttpContext.Current.Request["GroupNewsItemId"];
            FCKeditorShortContent = new FredCK.FCKeditorV2.FCKeditor();
            FCKeditorBody = new FredCK.FCKeditorV2.FCKeditor();
            string SelectLanguageText =
            "<select id=divSelectLanguage>";
            string[] Languages = Environment.Environments.GetLanguages();
            for (int iIndex = 0; iIndex < Languages.Length; iIndex++)
            {
                SelectLanguageText += "<option value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
            }
            SelectLanguageText += "</select>";

            AjaxPro.Utility.RegisterTypeForAjax(typeof(AddNews));
            string RetHtml =
                WebPartUtilities.AttachJavascript("Portal.CMS", "News.js") +
                             Environment.Environments.GetHeaderFormDialog(ResourceBLL.GetText(WebPartId,"AddNewsFormTitle","Thêm mới tin tức"))+
                             "<div class=cssFormContent>" +
                             "      <table>" +
                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "AddNewsSelectLanguage", "Ngôn ngữ") + "</td>" +
                             "              <td>"+SelectLanguageText+"</td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId,"AddNewsSubject","Tiêu đề") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtSubject></td>" +
                             "          </tr>" +                             
                             "          <tr>" +
                             //"              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "AddNewsSmallImage", "Ảnh đặc trưng") + "</td>" +
                             //"              <td><input class=cssTextBoxDialog id=txtImageUrl></td>" +
                             "          </tr>" +

                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "ShortContent", "Mô tả") + "</td>" +
                             "              <td><textarea class=cssTextBoxDialog id=\"txtShortContent\"></textarea></td>" +
                             "          </tr>" +

            //                 "          <tr>" +
            //                 "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId,"AddNewsShort","Mô tả") + "</td>" +
            //                 "          <td>";
            //pl.Controls.Add(new System.Web.UI.LiteralControl(RetHtml));
            //FCKeditorShortContent.Height = 400;
            //FCKeditorShortContent.BasePath = "fckeditor/";
            //FCKeditorShortContent.Width = 780;
            //pl.Controls.Add(FCKeditorShortContent);
            //RetHtml="</td>" +
            //                 "          </tr>" +
                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId,"AddNewsBody","Nội dung") + "</td>" +
                             "              <td class=cssTdFckEditor>";
            pl.Controls.Add(new System.Web.UI.LiteralControl(RetHtml));
            FCKeditorBody.Height=800;
            FCKeditorBody.Width = 780;
            FCKeditorBody.BasePath = "fckeditor/";
            pl.Controls.Add(FCKeditorBody);


            RetHtml =
                             "</td>" +
                             "          </tr>" +

                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "Keyword", "Từ khóa bài viết") + "</td>" +
                             "              <td><textarea class=cssTextBoxDialog id=txtSearchKeyword></textarea></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "Source", "Nguồn") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtSource></td>" +
                             "          </tr>" +

                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "Date", "Ngày tạo") + ":</td>" +
                             "              <td>" + Environment.LookUpUtilities.GenerateLookUpDate("txtEntryDate", System.DateTime.Now.ToString("dd/MM/yyyy")) + "</td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "Attached1", "File đính kèm ?") + "</td>" +
                             "              <td>Tài liệu <input class=cssAttached type=checkbox id=chkAttactedDoc></td>" +
                             "          </tr>" +

                             //"          <tr>" +
                             //"              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "Attached", "Gắn kèm ?") + "</td>" +
                             //"              <td>Ảnh <input class=cssAttached type=checkbox id=chkAttactedImage> ~ Video <input class=cssAttached type=checkbox id=chkAttactedVideo> ~ Tài liệu <input class=cssAttached type=checkbox id=chkAttactedDoc></td>" +
                             //"          </tr>" +

                             //"          <tr>" +
                             //"              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "EboVideo", "Nhúng video") + "</td>" +
                             //"              <td><input id=\"txtEmObjectVideo\"></td>" +
                             //"          </tr>" +
                             //"          <tr>" +
                             //"              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "EboAudio", "Nhúng Audio") + "</td>" +
                             //"              <td><input id=\"txtEmObjectAudio\"></td>" +
                             //"          </tr>" +
                             "          <tr>" +
                             "              <td class=cssCol1></td>" +
                             "              <td><input type=button class=cssSaveButton onclick=javascript:ClientSaveAddNews('" + GroupNewsId + "','" + GroupNewsItemId + "'); value=\"" + ResourceBLL.GetText(WebPartId, "AddNewsSaveButton", "Ghi lại") + "\"><input type=button class=cssCancelButton onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "AddNewsCancelButton", "Thoát") + "\"></td>" +
                             "          </tr>" +
                             "      </table>" +
                             "</div>"+Environment.Environments.GetFooterFormDialog();
            pl.Controls.Add(new System.Web.UI.LiteralControl(RetHtml));
        }



        [AjaxPro.AjaxMethod]
        public string ServerSideAddNews(
            string ActiveLanguage, 
            string GroupNewsId, 
            string GroupNewsItemId, 
            string Subject, 
            string ImageUrl, 
            string ShortContent, 
            string Body, 
            string SearchKeyword, 
            string Source,
            string sEntryDate,
            bool AttachedDoc
            //bool AttachedImage,
            //bool AttachedVideo,
            //string EmbVideo,
            //string EmbAudio
            )
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                if (Subject.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId,"EnterSubject","Nhập tiêu đề!"));
                //if (ShortContent.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId,"InputShortContent","Nhập mô tả!"));
                if (Body.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId,"InputBody","Nhập nội dung!"));
                bool CheckOk=Environment.FuncUtilities.checkVnDate(sEntryDate);
                
                if(CheckOk==false)throw new Exception(ResourceBLL.GetText(WebPartId,"InvalidDate","Ngày tháng nhập sai!"));
                DateTime EntryDate = Environment.FuncUtilities.VNDateToDate(sEntryDate);

                ShortContent = System.Web.HttpUtility.HtmlDecode(ShortContent);
                Body = System.Web.HttpUtility.HtmlDecode(Body);

                string NewsId = new NewsBLL().AddNews(null, ActiveLanguage, Subject, ImageUrl, ShortContent, Body, GroupNewsId, GroupNewsItemId, "", "", Environment.Environments.CurrentLoginUser.LoginName, SearchKeyword, Source, EntryDate, AttachedDoc ? 1 : 0);//, AttachedVideo ? 1 : 0, AttachedImage ? 1 : 0,EmbVideo, EmbAudio);
                return NewsId;// 
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetAddOkMsg()
        {
            return ResourceBLL.GetText(WebPartId, "MessageAddOk", "Thêm thành công!");
        }


        [AjaxPro.AjaxMethod]
        public string GetConfirmUploadImage()
        {
            try
            {
                return ResourceBLL.GetText(WebPartId, "ConfirmUploadImageAfterAddNews", "Thêm mới tin thành công! Bạn có muốn upload ảnh đặc trưng không?");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetUpdateImageUrl(string NewsId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);

                return Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new UploadImage().WebPartId),
                        new ParamCls("NewsId",NewsId)
                    });
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

    }
}